In this demo, we'll explore the principle
of least privilege. We will create a
Cloud Run service that uses the default
service account. We will then recreate
the same service, but using a dedicated
service account and add a policy binding to the IAM policy of the authenticated service with
the role Cloud Run invoke. This will enable us to adhere to the policy
of least privilege. Our first step is we're
going to go to our menu, and we are going to go
to our Marketplace, and we're going to
look for Cloud Run here to find our API. I'm going to click
on our Cloud Run API and we are going to enable, that just will
allow us to create the services we want
to try this demo. Now we're going to return home, and we are going to deploy
a simple Cloud Run service, so we're going to
open up Cloud Shell and we are going
to go on Deploy. We're going to call our service Shutdown an we're
going to pull from the image Cloud Run book and specifically the shutdown. We are going to
authorize or Cloud Shell and we are going to
select our region. I now select US Sector 1. We are going to allow
unauthenticated invocation. We'll let this Cloud
Run service be created. Our service is now provisioned. We can see if we go to
our menu and Cloud room, we can see our shutdown
service is available. We click into details, we can see the URL which
is also mentioned in our Cloud Shell down here,
and we click on that. It will show us our very
simple Cloud Run service, which has a single button
saying delete this service. If I click this, it will tell me that my
service has been deleted. Then refresh the
page to a 404 error. If I return to my Cloud Run, I can see that the
service has been deleted. Now, as you might imagine, this is a very powerful action. We may not want to
actually delete a service that's live
on serving customers. If we look to see
how this happens; look at the source
code for this, we can see that when we press
the button on the service, it calls the Cloud Run API, sending in our region and
our project ID and it calls delete method and allows this because we're using the
default service account, which essentially has
administrator access. We didn't specify
a service account. Due to project hierarchy, Cloud Run inherits the editor
role from the project, meaning it has access to a wide range of very powerful and possibly
very disruptive actions. This is one of those
basic roles that is very powerful and it really doesn't enforce the principle
of least privilege. This is something we
don't want to replicate. We can shut that down. Let's
return to our Cloud Shell. What we're going to do
here is we're going to try to recreate
the same service. But when we're creating it, we're going to deploy to a specific service
account so that we can enforce the principle
of least privilege by giving only the permissions that are needed for this application. The first thing we
need to do is actually create our service account. I'm going to call
it shutdown SVC. It has created our
service account and we can verify those there by listing all of
our service accounts. We can see that our
shutdown SVC is the top one here and that's an email we'll need
to hang onto for our next command, so I
was going to copy it. Now that we've created
our service account, we're going to redeploy the same service but we're going to specifically specify
the service account we want for the service. We will use the same command. I'm actually over here. I'm going to run
deploy, same name again, shutdown, same image. We're going for
gco.io/cloud-run-book/shutdown. But we're also then going to
specify our service account. Here's where we put
in the email address that was just created
for our service account. This will go through
the same process. It'll ask us for
a region will put the same region, US Central 1. It'll ask us, are we going to allow unauthenticated
invocations? Since this service will
run in the Internet, yes, that makes sense
for what we've created. That span up for us. Now we have our Cloud run
service has been created again. If I refresh this page, we can see that shutdown
has been recreated. I need to open up my
Cloud shell again. If we click on our link to
our service, we can see it. But what happens now is when
I call delete this service, I get a 403 error
because we do not have the authorization to
actually run this command. We haven't assigned
any policy bindings to our service account
and as a result, they don't have
permissions to do any actions on our service. We started off with the
default service account, which was too broad, it allowed us to do
almost everything to their cloud service. We then created a
dedicated service account, but with no policy bindings which is the opposite
end of the spectrum, we cannot do anything. What we're going to do now
is we're going to deploy a policy binding to
the service account. I'm going to run
gcloud run services and-iam-policy-binding. I write call shutdown that's a
service that I have. The member is going to be my service account.
Spell member correctly. We'll put in the email address of the service
Cloud that we had. Now we need to specify
the role for that member. The role we're
going to give them is our Cloud run admin role, which is where our delete
action will come from. I'll run this command now. Again, we need to
pick our region, so I'll stick with
the same region. Now it has applied to
that policy binding to our existing service. It has also given us a breakdown of the bindings that are relevant
on the service. We can see that we have
our service account here. It has the roles run admin, but in our members we
also have the third kind. When we talked about
our iam users, we had our basic roles. We had our predefined roles, we had our custom
roles, and then we had that special
category, all users. That makes a lot
of sense because this app is going
to be available in the Internet and we're allowing
all authenticated access, which means anybody has the role of run
invokers so they can invoke a command to
our Cloud run service. Now that we have updated this, if I return to our
app that was running, that gave us a 403
error last time, we now got our service
deleted because it has the correct privileges
to be allowed to carry out that request. You can see in this
demo that allowing a default service account gives too many privileges to
our cloud run service. We need to be mindful of our principle of
least privilege. The correct way of
approaching this is to create a dedicated service account for Cloud run service and to grant only the permissions that are required to run that
serve correctly.